package com.hudsonmq.spring.utils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 拓展點:
 * 1. 連接NameSrv 實時獲取 Topic 可發送的隊列數,如果沒有,則默認為4
 * 2. 支持多種獲取策略,目前默認輪詢
 */
public class QueueIdUtil {
    private static final ConcurrentHashMap<String, AtomicInteger> queueIdMap = new ConcurrentHashMap<>();

    public static int getQueueId(String topic){
        AtomicInteger nowIndex = queueIdMap.computeIfAbsent(topic, k -> new AtomicInteger());
        return nowIndex.getAndIncrement() % 4;
    }
}
